from celery_app import celery_app
from tasks import *
import time
import sys

if __name__ == "__main__":
    # 启动 Celery Worker，使用 solo pool 避免 Windows 上的权限问题
    max_retries = 5
    retry_count = 0
    
    while retry_count < max_retries:
        try:
            print(f"尝试启动 Celery Worker (尝试 {retry_count + 1}/{max_retries})")
            celery_app.worker_main(["worker", "--loglevel=info", "--pool=solo"])
            break  # 成功启动则退出循环
        except Exception as e:
            print(f"Celery Worker 启动失败: {e}")
            retry_count += 1
            if retry_count < max_retries:
                print(f"等待 5 秒后重试...")
                time.sleep(5)
            else:
                print("达到最大重试次数，退出程序")
                sys.exit(1)